﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Brain
{
    public class MenuSystemManager: IBaseComponent, IUpdateable, IDrawable
    {
        public bool ShouldDraw { get; set; }
        public bool ShouldUpdate { get; set; }

        public List<MenuSystem> Menus { get; set; }

        public MenuSystem CurrentMenu;

        public IMenuTransition Transition;

        public bool BlockInput = false;

        public MenuSystemManager()
        {
            Menus = new List<MenuSystem>();

            ShouldDraw = true;
            ShouldUpdate = true;
        }

        /// <summary>
        /// The first menu to be submited here
        /// will be the Active One
        /// </summary>
        /// <param name="menu"></param>
        public void SubmitMenu(MenuSystem menu)
        {
            if (this.CurrentMenu == null)
            {
                this.CurrentMenu = menu;
            }
            Menus.Add(menu);
        }

        public void ChangeTo(MenuSystem menu)
        {
            this.CurrentMenu = menu;
            this.CurrentMenu.CanSelect = true;
            this.Transition.Reset();
        }

        private MenuSystem transitionTo;
        public void TransitionTo(MenuSystem menu)
        {
            if (CurrentMenu == null ||
                this.Transition == null)
            {
                throw new Exception("There's no Transition / no Menu to transite to!");
            }
            if (CurrentMenu == menu)
            {
#if DEBUG
                Debug.WriteLine("You're trying to transition from the same screen to the same screen! at TransitionTo on MenuSystemManager");
#endif
                return;
            }
            this.transitionTo = menu;
            this.Transition.Reset();
            CurrentMenu.CanSelect = false;
            transitionTo.CanSelect = false;
            IsTransitioning = true;
        }

        public bool IsTransitioning { get; set; }

        public void Update()
        {
            if (this.CurrentMenu != null)
            {
                if (CurrentMenu.ShouldUpdate && !BlockInput)
                {
                    CurrentMenu.Update();
                }
            }
            if (this.transitionTo != null && Transition != null)
            {
                Transition.TransitioningFrom(this.CurrentMenu);
                bool canSelect;
                if (Transition.TransitioningTo(this.transitionTo, out canSelect))
                {
                    this.CurrentMenu = this.transitionTo;
                    this.transitionTo = null;
                    this.CurrentMenu.CanSelect = true;
                    IsTransitioning = false;
                }
                else
                {
                    this.transitionTo.CanSelect = canSelect;
                    if (canSelect && !BlockInput)
                    {
                        transitionTo.Update();
                    }
                }
            }
        }
        public void Draw()
        {
        }

        public string Name { get; set; }
        public bool ShouldSerialize { get; set; }
        public void SerializeData(SerializationData parentData, string index)
        {
        }
        public void DeserializeData(SerializationData parentData, string index)
        {
        }


        public void DeserializedAll()
        {
        }
    }
}
